package kr.ac.kaist.savehearts.ui;

import kr.ac.kaist.savehearts.Configure;
import kr.ac.kaist.savehearts.SaveHeartsConstants;
import kr.ac.kaist.savehearts.location.UserLocationTracker;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Switch;
import android.widget.TextView;

public class GPSSettingsActivity extends Activity 
	implements CompoundButton.OnCheckedChangeListener, OnSeekBarChangeListener, OnClickListener {
	
	private static final String TAG = GPSSettingsActivity.class.getSimpleName();

	private Switch switchGPS;
	private TextView textViewInterval;
	private SeekBar seekbarInterval;
	private Configure configure = Configure.getInstance();
	
	private Button buttonOk;
	private Button buttonBack;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gps_settings);
		
		buttonOk = (Button)findViewById(R.id.buttonGPSSettingsOk);
		buttonOk.setOnClickListener(this);
		buttonBack = (Button)findViewById(R.id.buttonGPSSettingsBack);
		buttonBack.setOnClickListener(this);
		
		switchGPS = (Switch)findViewById(R.id.switchGPSPower);
		switchGPS.setOnCheckedChangeListener(this);
		
		textViewInterval = (TextView)findViewById(R.id.textViewGPSInterval);
		
		seekbarInterval = (SeekBar)findViewById(R.id.seekBarGPSInterval);
		seekbarInterval.setOnSeekBarChangeListener(this);
		seekbarInterval.setMax(SaveHeartsConstants.GPS_MAX_INTERVAL - SaveHeartsConstants.GPS_MIN_INTERVAL);
		
		switchGPS.setChecked(configure.isGpsOn());
		seekbarInterval.setProgress(configure.getGPSInterval() - SaveHeartsConstants.GPS_MIN_INTERVAL);
		seekbarInterval.setEnabled(configure.isGpsOn());
	}
	
	/**
	 * Change GPS settings
	 */
	private void setGPSSettings() {
		Log.d(TAG, "setGPSSettings()");
		UserLocationTracker userTracker = UserLocationTracker.getInstance(this);
		if ( userTracker.isTrackerRunning() ) userTracker.stopTracking();
		
		configure.isGpsOn(switchGPS.isChecked());
		// change gps update interval when gps is turned on
		if ( configure.isGpsOn() ) {
			configure.setGPSInterval(seekbarInterval.getProgress());
			userTracker.setUpdateInterval(configure.getGPSInterval() * 
					SaveHeartsConstants.MIN_IN_MS);
			userTracker.startTracking();
		} 
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if ( seekbarInterval != null )
			seekbarInterval.setEnabled(isChecked);
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		// update textView which shows update duration (e.g., 5분)
		int newInterval = progress + SaveHeartsConstants.GPS_MIN_INTERVAL;
		if ( textViewInterval != null )
			textViewInterval.setText(newInterval + " " + getString(R.string.minute));
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// do nothing...
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		// do nothing...
	}

	@Override
	public void onClick(View v) {
		switch ( v.getId() ) {
			case R.id.buttonGPSSettingsOk:
				setGPSSettings();
				finish();
				break;
			case R.id.buttonGPSSettingsBack:
				finish();
				break;
		}
	}
	
	
}
